const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const rmfr = require('rmfr')
const jsTranser = require('./jsTranser');
const lessTranser = require('./lessTranser');
const publishTask = require('./lib/publish');
const localdev = require('./lib/localdev');
function tasks(config = {}) {
    const { jsPaths, dest, lessPaths } = config;
    return new Promise((resolve) => {
        let fn = gulp.series(
            () => { return lessTranser(dest, lessPaths) },
            () => { return jsTranser(dest, jsPaths) },
            () => { resolve() }
        );
        fn();
    })
}

class buz {
    constructor(options = {}) {
        const { dest, lessPaths, jsPaths, publishTag, publishSrc, debugProjectSrc } = options;
        this.config = {
            lessPaths,
            dest,
            jsPaths,
            publishTag,
            publishSrc,
            debugProjectSrc,
        }
    }
    build = async () => {
        await rmfr(this.config.dest);
        console.log(`remove ${this.config.dest}`);
        await tasks(this.config);
        console.log('copy package.json');
        fs.copyFileSync(path.resolve(process.cwd(), 'package.json'), path.resolve(this.config.dest, 'package.json'));
        fs.copyFileSync(path.resolve(process.cwd(), 'README.md'), path.resolve(this.config.dest, 'README.md'));
        console.log('Build task done!');
    }
    publish = () => {
        publishTask(this.build, this.config.publishSrc, { tag: this.config.publishTag })
    }
    dev = () => {
        this.build().then(() => {
            localdev(this.config.dest, this.config.debugProjectSrc);
        });
    }
}
module.exports = buz;